package cn.okuribito.config.quartz;

import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.TriggerKey;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author liubuhe
 * @since 10:38-2021/6/12
 */
@Component
public class WebSocketScheduler {

    @Resource
    private Scheduler scheduler;

    public void acceptJob(WebSocketJob job) {
        try {
            if (!this.scheduler.checkExists(job.returnJobKey())) {
                this.scheduler.scheduleJob(job.returnJobDetail(), job.returnTrigger());
            } else {
                this.scheduler.deleteJob(job.returnJobKey());
                this.scheduler.scheduleJob(job.returnJobDetail(), job.returnTrigger());
            }
        } catch (SchedulerException var3) {
            var3.printStackTrace();
        }

    }

    public void deleteJob(TriggerKey tk) {
        try {
            if (this.scheduler.checkExists(tk)) {
                this.scheduler.unscheduleJob(tk);
            }
        } catch (SchedulerException var3) {
            var3.printStackTrace();
        }

    }
}
